package com.ssh.demo.netty.handler;

import com.ssh.demo.helper.ByteArrHelper;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName TcpSessionHandler
 * @Description TODO
 * @Author ssh
 * @Date 2021/4/12 13:56
 */
@Slf4j
@AllArgsConstructor
public class TcpSessionHandler extends ChannelInboundHandlerAdapter {

    private ByteArrHelper byteArrHelper;

    /**
     * 报错触发
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.debug("session exception with {}", ctx.name());
        log.warn(cause.getMessage());
        // 异常时 关闭session 等待重连
        ctx.close();
    }

    /**
     * 客户端断开连接触发方法
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        byte[] originData  = (byte[])msg;
        String s = byteArrHelper.toHexString(originData);
        System.out.println("处理参数"+s);

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }


}
